#!/usr/bin/env python

"""
case.cmpgen_namelists - perform namelist baseline operations (compare,
generate, or both) for this case.
"""

from standard_script_setup import *

from CIME.case           import Case
from argparse            import RawTextHelpFormatter

###############################################################################
def parse_command_line(args, description):
###############################################################################
    parser = argparse.ArgumentParser(
        description=description,
        formatter_class=RawTextHelpFormatter)

    CIME.utils.setup_standard_logging_options(parser)

    parser.add_argument("caseroot", nargs="?", default=os.getcwd(),
                        help="Case directory for which namelists are compared/generated. "
                        "\nDefault is current directory.")

    parser.add_argument("-c", "--compare", action="store_true",
                        help="Force a namelist comparison against baselines. "
                        "\nDefault is to follow the case specification.")

    parser.add_argument("-g", "--generate", action="store_true",
                        help="Force a generation of namelist baselines. "
                        "\nDefault is to follow the case specification.")

    parser.add_argument("--compare-name",
                        help="Force comparison to use baselines with this name. "
                        "\nDefault is to follow the case specification.")

    parser.add_argument("--generate-name",
                        help="Force generation to use baselines with this name. "
                        "\nDefault is to follow the case specification.")

    parser.add_argument("--baseline-root",
                        help="Root of baselines. "
                        "\nDefault is the case's BASELINE_ROOT.")

    args = CIME.utils.parse_args_and_handle_standard_logging_options(args, parser)

    return args.caseroot, args.compare, args.generate, args.compare_name, args.generate_name, args.baseline_root

###############################################################################
def _main_func(description):
###############################################################################
    caseroot, compare, generate, compare_name, generate_name, baseline_root \
        = parse_command_line(sys.argv, description)
    with Case(caseroot, read_only=False) as case:
        success = case.case_cmpgen_namelists(compare, generate, compare_name, generate_name, baseline_root)

    sys.exit(0 if success else CIME.utils.TESTS_FAILED_ERR_CODE)

if __name__ == "__main__":
    _main_func(__doc__)
